package com.shouhe.mysap.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shouhe.mysap.common.CreateVerifiCodeImage;
import com.shouhe.mysap.common.LoginForm;
import com.shouhe.mysap.common.PersonLogin;
import com.shouhe.mysap.entity.SalePerson;
import com.shouhe.mysap.service.SalePersonService;
import com.shouhe.mysap.util.PersonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 守鹤
 * @date 2020/5/31 22:50
 */
@Controller
@RequestMapping("/system")
public class SystemController {

    @Autowired
    private SalePersonService salePersonService;

    //存储返回给页面的对象数据
    private Map<String, Object> result = new HashMap<>();

    /**
     * @描述 得到自己的名字
     * @参数
     * @返回值
     */
    @RequestMapping(value = "/getUserName", produces = MediaType.TEXT_PLAIN_VALUE + ";charset=utf-8")
    @ResponseBody
    public String getUserName(HttpServletRequest request) {
        Object userInfo =  request.getSession().getAttribute("userInfo");
        PersonLogin login = PersonUtil.getPerosnLogin(userInfo);
        System.out.println(login);
        return login.getUserName();
    }

    /**
     * @description: 获取并显示验证码图片
     * @param: request
     * @param: response
     * @date: 2019-06-09 6:34 PM
     * @return: void
     */
    @GetMapping("/getVerifiCodeImage")
    public void getVerifiCodeImage(HttpServletRequest request, HttpServletResponse response) {
        // 验证码图片
        BufferedImage verifiCodeImage = CreateVerifiCodeImage.getVerifiCodeImage();
        // 验证码
        String verifiCode = String.valueOf(CreateVerifiCodeImage.getVerifiCode());
        // 存储验证码Session
        request.getSession().setAttribute("verifiCode", verifiCode);
        // 将验证码图片输出到登录页面
        try {
            ImageIO.write(verifiCodeImage, "JPEG", response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * @description: 验证用户登录信息
     * @param: loginForm
     * @param: request
     * @date: 2019-06-19 7:05 PM
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     */
    @PostMapping("/login")
    @ResponseBody
    public Map<String, Object> login(LoginForm loginForm, HttpServletRequest request) {
        System.out.println(loginForm);
        String vcode = (String) request.getSession().getAttribute("verifiCode");
        if ("".equals(vcode)) {
            result.put("success", false);
            result.put("msg", "长时间为操作,会话已失效,请刷新页面后重试!");
            return result;
        } else if (!loginForm.getVerifiCode().equalsIgnoreCase(vcode)) {
            result.put("success", false);
            result.put("msg", "验证码错误!");
            return result;
        }
        request.getSession().removeAttribute("verifiCode");
        //得到身份类型
        Integer userType = loginForm.getUserType();
        if (userType == 1) {
            QueryWrapper<SalePerson> wrapper=new QueryWrapper<>();
            wrapper.eq("sale_name",loginForm.getUsername()).eq("sale_pwd",loginForm.getPassword());
            SalePerson obj =(SalePerson) salePersonService.getObj(wrapper);
            if (obj != null) {
                HttpSession session = request.getSession(); //将用户信息存储到Session
                session.setAttribute("userInfo", obj);
                session.setAttribute("userType", loginForm.getUserType());
                result.put("success", true);
                result.put("msg", "登录成功");
                return result;
            }
        }
        //登录失败
        result.put("success", false);
        result.put("msg", "用户名或密码错误!");
        return result;
    }


    /**
     * @description: 注销用户信息
     * @param: request
     * @date: 2019-06-11 6:40 PM
     * @return: void
     */
    @GetMapping("/loginOut")
    public void loginOut(HttpServletRequest request, HttpServletResponse response) {
        request.getSession().removeAttribute("userInfo");
        request.getSession().removeAttribute("userType");

        //注销后重定向到登录页面
        try {
            response.sendRedirect("/go/index");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
